import java.awt.EventQueue;
import java.awt.Scrollbar;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JLabel;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class newGame extends JFrame {

	private JPanel contentPane;
	private File dictionaryFile;
	private JTextField dictionaryName;
	private JTextField addPlayer;
	private DefaultListModel playerListModel;
	private JList playerList;
	private JComboBox boardSizeComboBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					newGame frame = new newGame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public newGame() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 174, 409);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setLocationRelativeTo(null);

		//Board Size
		{
			//Label
			JLabel lblBoardSize = new JLabel("Board Size:");
			lblBoardSize.setFont(new Font("Tahoma", Font.BOLD, 11));
			lblBoardSize.setBounds(10, 11, 138, 14);
			contentPane.add(lblBoardSize);

			//Combo Box
			String [] boardSizes = {"4x4", "5x5", "6x6", "7x7", "8x8"};
			boardSizeComboBox = new JComboBox(boardSizes);
			boardSizeComboBox.setBounds(10, 29, 138, 20);
			contentPane.add(boardSizeComboBox);
		}

		//Dictionary
		{			
			//Label
			JLabel lblDictionary = new JLabel("Dictionary:");
			lblDictionary.setFont(new Font("Tahoma", Font.BOLD, 11));
			lblDictionary.setBounds(10, 60, 138, 14);
			contentPane.add(lblDictionary);

			//Text Field
			dictionaryName = new JTextField("No Dictionary Selected");
			dictionaryName.setHorizontalAlignment(SwingConstants.CENTER);
			dictionaryName.setEditable(false);
			dictionaryName.setBounds(10, 85, 138, 20);
			contentPane.add(dictionaryName);
			dictionaryName.setColumns(10);

			//Button
			JButton btnBrowse = new JButton("Browse Dictionary");
			btnBrowse.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JFileChooser chooser = new JFileChooser();
					chooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
					if(chooser.showOpenDialog(getParent()) == JFileChooser.APPROVE_OPTION){
						dictionaryFile = chooser.getSelectedFile();
						dictionaryName.setText(dictionaryFile.getName());
					}
				}
			});
			btnBrowse.setBounds(10, 116, 138, 23);
			contentPane.add(btnBrowse);
		}

		//Players
		{
			//Label
			JLabel lblPlayers = new JLabel("Players:");
			lblPlayers.setFont(new Font("Tahoma", Font.BOLD, 11));
			lblPlayers.setBounds(10, 150, 138, 14);
			contentPane.add(lblPlayers);

			//Player List
			playerListModel = new DefaultListModel();
			playerList = new JList(playerListModel);
			playerList.setBounds(10, 206, 138, 71);
			contentPane.add(playerList);

			//Scroll Bar
			final JScrollPane playerScroll = new JScrollPane(playerList);
			playerScroll.setBounds(playerList.getBounds());
			getContentPane().add(playerScroll);
			
			//Add Player Text Field
			addPlayer = new JTextField();
			addPlayer.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					addPlayerToList();
					playerScroll.getVerticalScrollBar().setValue(playerScroll.getVerticalScrollBar().getMaximum());
				}
			});
			addPlayer.setBounds(10, 175, 87, 20);
			contentPane.add(addPlayer);
			addPlayer.setColumns(10);

			//Add Button
			JButton addButton = new JButton("+");
			addButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					addPlayerToList();
				}
			});
			addButton.setBounds(107, 175, 41, 23);
			contentPane.add(addButton);

			//Remove Button
			JButton removeBtn = new JButton("Remove Player");
			removeBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(playerList.getSelectedIndex() >= 0)
						playerListModel.remove(playerList.getSelectedIndex());
				}
			});
			removeBtn.setBounds(10, 288, 140, 23);
			contentPane.add(removeBtn);
		}

		//Start Button
		JButton btnStartGame = new JButton("Start Game");
		btnStartGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//Valid Test
				Boolean isValid = true;
				//Parameters
				int boardSize;
				List<String> wordList = new ArrayList<String>();
				List<player> playerList = new ArrayList<player>();

				//Board Size
				switch(boardSizeComboBox.getSelectedIndex()){
				case 1: boardSize = 5; break;
				case 2: boardSize = 6; break;
				case 3: boardSize = 7; break;
				case 4: boardSize = 8; break;
				default: boardSize = 4; break;
				}

				//Dictionary Checker
				if(dictionaryFile == null){
					isValid = false;
					JOptionPane.showMessageDialog(getParent(),"Please select a dictionary");
				}

				//Player Checker
				if(isValid && playerListModel.getSize() < 1){
					isValid = false;
					JOptionPane.showMessageDialog(getParent(),"Please add a player");
				}

				//Load Dictionary
				if (isValid){
					Scanner words;
					try {
						words = new Scanner (dictionaryFile);
						//Checker for empty line
						while(words.hasNextLine()){
							String newWord = words.nextLine().trim();
							if (!(newWord.isEmpty()))
								wordList.add(newWord.toUpperCase());
						}
					} catch (FileNotFoundException e1) {
						JOptionPane.showMessageDialog(getParent(),"Error loading the dictionary");
						isValid = false;
						e1.printStackTrace();
					}
				}

				//Player List
				if(isValid){
					for(int i = 0; i < playerListModel.getSize(); i++){
						playerList.add(new player((String) playerListModel.get(i)));
					}
				}

				//New Boggle Game
				if(isValid){
					boggleMainGame newBoggleGame = new boggleMainGame(new boggleGame(boardSize, wordList, playerList));
					dispose();
				}
			}
		});
		btnStartGame.setBounds(10, 322, 138, 40);
		contentPane.add(btnStartGame);
	}

	private void addPlayerToList(){
		Set<String> playerSet = new LinkedHashSet<String>();

		for(int i = 0; i < playerListModel.getSize(); i++){
			playerSet.add((String)playerListModel.getElementAt(i));
		}
		if(!(addPlayer.getText().trim().isEmpty())){
			playerSet.add(addPlayer.getText().trim());
		}
		playerListModel = new DefaultListModel();
		for(String player : playerSet){
			playerListModel.addElement(player);
		}
		playerList.setModel(playerListModel);
		addPlayer.setText("");
	}
}
